package com.application.camera;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CapturePreview extends SurfaceView implements SurfaceHolder.Callback {
	private Camera mCamera;
	SurfaceHolder mHolder;

	public CapturePreview(Context context) {
	    super(context);
	    init();
	   
	}

	public CapturePreview(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CapturePreview(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	private void init(){
			mHolder=getHolder();
		    mHolder.addCallback(this);
		    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	public void surfaceCreated(SurfaceHolder holder) {
	    // The Surface has been created, acquire the camera and tell it where
	    // to draw.
	    mCamera = Camera.open();
	    try {
	       mCamera.setPreviewDisplay(holder);
	    } catch (IOException exception) {
	        mCamera.release();
	        mCamera = null;
	        // TODO: add more exception handling logic here
	    }
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
	    // Surface will be destroyed when we return, so stop the preview.
	    // Because the CameraDevice object is not a shared resource, it's very
	    // important to release it when the activity is paused.
	    mCamera.stopPreview();
	    mCamera = null;
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
	    // Now that the size is known, set up the camera parameters and begin
	    // the preview.
	    Camera.Parameters parameters = mCamera.getParameters();
	    parameters.setPreviewSize(w, h);
	    mCamera.setParameters(parameters);
	    mCamera.startPreview();
	}


}
